<?php


use Phinx\Migration\AbstractMigration;

class FillMenuData extends AbstractMigration
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    /**
     * Migrate Up.
     * @throws Exception
     */
    public function up()
    {
        $rows = [
            [
                'name' => '工作台',
                'code' => 'workbench',
                'icon' => 'workbench_icon',
                'menu_list' => [
                    [
                        'name' => '工作台',
                        'code' => 'workbench',
                        'icon' => '',
                        'route' => '/profile/workbench'
                    ]
                ]
            ],
            [
                'name' => '审片室',
                'code' => 'audit',
                'icon' => 'audit_icon',
                'menu_list' => [
                    [
                        'name' => '审片室',
                        'code' => 'audit',
                        'icon' => '',
                        'route' => '/profile/audit'
                    ]
                ]
            ],
            [
                'name' => '运营后台',
                'code' => 'backend_manage',
                'icon' => 'backend_manage_icon',
                'menu_list' => [
                    [
                        'name' => '默认配置',
                        'code' => 'backend_manage',
                        'icon' => '',
                        'route' => '/backend/options'
                    ]
                ]
            ],
            [
                'name' => '团队管理',
                'code' => 'group_manage',
                'icon' => 'group_manage_icon',
                'menu_list' => [
                    [
                        'name' => '团队管理',
                        'code' => 'group_manage',
                        'icon' => '',
                        'route' => '/profile/group'
                    ]
                ]
            ],
            [
                'name' => '采购管理',
                'code' => 'purchase_manage',
                'icon' => 'purchase_manage_icon',
                'menu_list' => [
                    [
                        'name' => '采购管理',
                        'code' => 'purchase_manage',
                        'icon' => '',
                        'route' => '/profile/purchase'
                    ]
                ]
            ],
            [
                'name' => '任务分配',
                'code' => 'task_assign',
                'icon' => 'task_assign_icon',
                'menu_list' => [
                    [
                        'name' => '任务分配',
                        'code' => 'task_assign',
                        'icon' => '',
                        'route' => '/profile/headman'
                    ]
                ]
            ],
            [
                'name' => '创建项目',
                'code' => 'create_project',
                'icon' => 'create_project_icon',
                'menu_list' => [
                    [
                        'name' => '创建项目',
                        'code' => 'create_project',
                        'icon' => '',
                        'route' => '/project/list'
                    ]
                ]
            ],
            [
                'name' => '项目管理',
                'code' => 'project_manage',
                'icon' => 'project_manage_icon',
                'menu_list' => [
                    [
                        'name' => '详情',
                        'code' => 'project_details',
                        'icon' => '',
                        'route' => '/project/detail'
                    ],
                    [
                        'name' => '清单',
                        'code' => 'project_inventory',
                        'icon' => '',
                        'route' => '/project/inventory'
                    ],
                    [
                        'name' => '计划',
                        'code' => 'project_plan',
                        'icon' => '',
                        'route' => '/project/plan'
                    ]
                ]
            ]
        ];


        // 写入数据库
        $menuModuleTable = $this->table('menu_module');
        $menuRouteTable = $this->table('menu_route');

        $index = 1;
        foreach ($rows as $item) {
            // 先写入菜单模块
            $menuModuleData = [
                'name' => $item['name'],
                'code' => $item['code'],
                'icon' => $item['icon'],
                'index' => $index,
                'uuid' => Webpatser\Uuid\Uuid::generate()->string
            ];


            $menuModuleTable->insert($menuModuleData)->save();


            // 获取当前写入菜单模块 id
            $query = $this->fetchRow('SELECT max(`id`) as id FROM `menu_module`');

            // 写入模块菜单路由
            $routeIndex = 1;
            foreach ($item["menu_list"] as $routeItem) {

                $routeItem['menu_module_id'] = $query['id'];
                $routeItem['uuid'] = Webpatser\Uuid\Uuid::generate()->string;
                $routeItem['index'] = $routeIndex;

                $menuRouteTable->insert($routeItem)->save();

                $routeIndex++;
            }

            $index++;
        }
    }

    /**
     * Migrate Down.
     */
    public function down()
    {
        $this->execute('DELETE FROM menu_module');
        $this->execute('DELETE FROM menu_route');
    }
}
